<template>
  <div class="app-container">
    <el-card>
      <div style="display: flex; align-items: center; margin-bottom: 10px">
        <div style="font-weight: bold">合同所属公司类别管理</div>
        <div style="margin-left: auto">
          <el-button
            v-for="(item, index) in buttonList"
            style="height: 40px"
            :icon="item.icon"
            :key="index"
            :type="item.type"
            @click="item.methods"
            >{{ item.text }}</el-button
          >
          <el-button
            style="height: 40px; width: 40px; padding: 0"
            icon="el-icon-refresh"
            @click="getList()"
          />
        </div>
      </div>
      <el-table
        v-loading="loading"
        :header-cell-style="{
          background: 'rgb(224, 241, 255)',
          color: '#606266',
        }"
        style="width: 100%; border: 1px solid #ccc"
        :data="BelongingCompanyList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column align="center" type="expand">
          <template slot-scope="scope">
            <el-table
              :data="scope.row.contractCategoryList"
              style="width: 100%"
              :fit="true"
              :header-cell-style="{
                background: 'rgb(224, 241, 255)',
                color: '#606266',
              }"
              :row-style="tableRowClassName"
            >
              <el-table-column prop="name" label="合同类别" align="center" />
              <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="停用"
                    placement="top"
                  >
                    <svg-icon
                      class-name="icon"
                      icon-class="stop"
                      style="height: 24px; width: 24px"
                      v-if="scope.row.state == 0"
                      @click="enable(scope.row)"
                    />
                  </el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="启用"
                    placement="top"
                  >
                    <svg-icon
                      class-name="icon"
                      icon-class="stop02"
                      v-if="scope.row.state == 1"
                      style="height: 24px; width: 24px"
                      @click="enable(scope.row)"
                    />
                  </el-tooltip>
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column label="合同所属公司" align="center" prop="name" />
        <el-table-column label="合同所属公司编码" align="center" prop="code" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              content="添加"
              placement="top"
            >
              <svg-icon
                class-name="icon"
                icon-class="add"
                style="height: 24px; width: 24px"
                @click="addRouter(scope.row)"
              />
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="编辑"
              placement="top"
            >
              <svg-icon
                class-name="icon"
                icon-class="editTable"
                style="height: 24px; width: 24px"
                @click="handleUpdate(scope.row)"
              />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改合同所属公司对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司名称" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入公司名称"
            maxlength="50"
            show-word-limit
            type="text"
            :class="{
              'input-limit-reached': form.name && form.name.length >= 50,
            }"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBelongingCompany,
  getBelongingCompany,
  delBelongingCompany,
  addBelongingCompany,
  updateBelongingCompany,
} from "@/api/contract/BelongingCompany";
import { updateContractCategory } from "@/api/contract/ContractCategory";
export default {
  name: "BelongingCompany",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同所属公司表格数据
      BelongingCompanyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
      },
      buttonList: [
        {
          icon: "el-icon-circle-plus-outline",
          type: "primary",
          text: "添加",
          methods: this.handleAdd,
        },
      ],
      // 表单参数
      form: {
        name: "",
      },
      // 表单校验
      rules: {
        name: [
          {
            required: true,
            message: "合同所属公司名称不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //新增按钮
    addRouter(row) {
      console.log(row);
      this.$router.push({
        path: "/system/BelongingCompany/addContracts",
        query: {
          belongingCompanyId: row.belongingCompanyId,
        },
      });
    },
    //启用方法
    enable(row) {
      console.log(row);
      let body = {
        contractCategoryId: row.contractCategoryId,
        name: row.name,
        state: row.state,
      };
      let text = "禁用";
      if (row.state == 0) {
        console.log("禁用");
        body.state = 1;
        text = "禁用";
      } else {
        console.log("启用");
        body.state = 0;
        text = "启用";
      }
      this.$modal
        .confirm("确认要" + text + '"' + row.name + '"' + "合同吗？")
        .then(() => {
          updateContractCategory(body);
        })
        .then(() => {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        })
        .catch(() => {});
    },
    /** 查询合同所属公司列表 */
    getList() {
      this.loading = true;
      listBelongingCompany(this.queryParams).then((response) => {
        this.BelongingCompanyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    tableRowClassName({ row, rowIndex }) {
      console.log(row.state);
      if (row.state == 1) {
        return {
          color: "rgba(154, 155, 200, 0.8)",
        };
      } else {
        return {};
      }
    },
    /** 查询合同类别列表 */
    getListByid() {
      this.loading = true;
      listContractCategory(this.queryParams).then((response) => {
        this.ContractCategoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        belongingCompanyId: null,
        name: null,
        code: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.belongingCompanyId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合同所属公司设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const belongingCompanyId = row.belongingCompanyId || this.ids;
      getBelongingCompany(belongingCompanyId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合同所属公司设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.code = null;
          if (this.form.belongingCompanyId != null) {
            updateBelongingCompany(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBelongingCompany(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const belongingCompanyIds = row.belongingCompanyId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除合同所属公司编号为"' +
            belongingCompanyIds +
            '"的数据项？'
        )
        .then(function () {
          return delBelongingCompany(belongingCompanyIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "contract/BelongingCompany/export",
        {
          ...this.queryParams,
        },
        `BelongingCompany_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
.icon {
  cursor: pointer;
  color: white;
  margin: 5px;
}
::v-deep .el-dialog {
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  margin: 0px !important;
}
::v-deep .el-dialog__header {
  background-color: rgb(248, 250, 251); /* 你可以根据需求修改颜色 */
  color: rgb(99, 86, 110); /* 标题文字颜色 */
}
/* 默认计数器颜色 */
.custom-input-counter .el-input__count {
  color: #909399;
}

/* 当达到最大长度时，计数器颜色变为红色 */
.input-limit-reached ::v-deep .el-input__count {
  color: red !important;
}
</style>
